package main.com.dhj.test02

object test06 {
    sealed abstract class Furniture

    case class Couch() extends Furniture

    case class Chair() extends Furniture

    def main(args: Array[String]): Unit = {

        val x = Couch()
        val y = Chair()
        println(findPlaceToSit(x))
        println(findPlaceToSit(y))

        println("-----------------------------------------");
        println(findPlaceToSit2(x))
        println(findPlaceToSit2(y))

        println("-----------------------------------------");
        println(findPlaceToSit3(x))
        println(findPlaceToSit3(y))

        println("-----------------------------------------");
        println(findPlaceToSit4(x))
        println(findPlaceToSit4(y))

    }


    def findPlaceToSit(piece: Furniture): String = piece match {
        case a: Couch => "Lie on the couch"
        case b: Chair => "Sit on the chair"
    }

    def findPlaceToSit2(piece: Furniture): String = {
        val a: Couch = Couch()
        val b: Chair = Chair()

        piece match {
            case a => "Lie on the couch 2"
            case b => "Sit on the chair 2"
        }
    }

    def findPlaceToSit3(piece: Furniture): String = piece match {
        case a: Couch => {
            // 这里 a 是匹配到的 Couch 实例
            println(s"内部可以访问 a.name：${a}")
            "Lie on the couch 3"
        }
        case b: Chair => {
            // 这里 b 是匹配到的 Chair 实例
            println(s"内部可以访问 b.name：${b}")
            "Sit on the chair 3"
        }
    }


    def findPlaceToSit4(piece: Furniture): String = piece match {
        case _: Couch => "Lie on the couch 4"
        case _: Chair => "Sit on the chair 4"

    }

}
